package cn.fintecher.gis.web;


import cn.fintecher.gis.entity.LayerGroup;
import cn.fintecher.gis.model.response.LayerGroupResponse;
import cn.fintecher.gis.respository.LayerGroupRepository;
import cn.fintecher.gis.service.LogInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@RestController
@RequestMapping("/api/layerGroup")
@Api(value = "图层组管理", description = "图层组管理")
public class LayerGroupController {

    @Autowired
    private LayerGroupRepository layerGroupRepository;

    @Autowired
    private LogInfoService logInfoService;

    @GetMapping("/getLayerGroupList")
    @ApiOperation(value = "查询图层组信息",notes = "查询图层组信息")
    public ResponseEntity<List<LayerGroup>> getLayerGroupList(){
        Sort sort = new Sort(Sort.Direction.DESC, "sort");
        List<LayerGroup> list = layerGroupRepository.findAll(sort);
        return ResponseEntity.ok().body(list);
    }

    @GetMapping("/getLayerGroupTree")
    @ApiOperation(value = "查询图层组信息(树形)", notes = "查询图层组信息")
    public ResponseEntity<List<LayerGroupResponse>> getLayerGroupTree() {
        Sort sort = new Sort(Sort.Direction.DESC, "sort");
        List<LayerGroup> list = layerGroupRepository.findAll(sort);
        List<LayerGroupResponse> responseList = new ArrayList<>();
        for (LayerGroup group : list) {
            LayerGroupResponse response = new LayerGroupResponse();
            BeanUtils.copyProperties(group, response);
            if (StringUtils.isEmpty(group.getParentId())) {
                responseList.add(response);
                for (LayerGroup layerGroup : list) {
                    LayerGroupResponse response1 = new LayerGroupResponse();
                    BeanUtils.copyProperties(layerGroup, response1);
                    if (StringUtils.isNotBlank(layerGroup.getParentId())) {
                        if (group.getId().equals(layerGroup.getParentId())) {
                            response1.setParentId(response);
                            responseList.add(response1);
                        }
                    }
                }
            }
        }
        return ResponseEntity.ok().body(responseList);
    }

    @PostMapping("/saveLayerGroup")
    @ApiOperation(value = "维护图层组信息",notes = "维护图层组信息")
    public ResponseEntity<List<LayerGroup>> saveLayerGroup(@RequestBody LayerGroup layerGroup){
        Boolean result = layerGroupRepository.existsById(layerGroup.getId());
        if(!result){
            layerGroupRepository.save(layerGroup);
        }else{
            Optional<LayerGroup> group = layerGroupRepository.findById(layerGroup.getId());
            LayerGroup layer = group.get();
            layer.setGroupName(layerGroup.getGroupName());
            if(layerGroup.getParentId() != null && !layerGroup.getParentId().trim().equals("")){
                layer.setParentId(layerGroup.getParentId());
            }
            layerGroupRepository.save(layer);
        }
        List<LayerGroup> list = layerGroupRepository.findAll();
        return ResponseEntity.ok().body(list);
    }

    @DeleteMapping("/deleteLayerGroup/{id}")
    @ApiOperation(value = "删除图层组信息",notes = "删除图层组信息")
    public ResponseEntity<List<LayerGroup>> deleteLayerGroup(@PathVariable String id){
        Optional<LayerGroup> optional = layerGroupRepository.findById(id);
        if (optional.isPresent()) {
            LayerGroup layerGroup = optional.get();
            logInfoService.addLog("LayerGroupController", "deleteLayerGroup",
                    "id=" + id, "删除的图层目录名称是:" + layerGroup.getGroupName());
            layerGroupRepository.delete(layerGroup);
        }
        List<LayerGroup> list = layerGroupRepository.findAll();
        return ResponseEntity.ok().body(list);
    }
}
